package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.KLineTitle;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.KLineTitleService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @Author: WangXing
 * @Description:K线栏目管理
 * @CreateDate: 2018/6/26 16:46
 */
@RestController
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"kline/")
@Api("K线栏目管理")
public class KLineTitleController {

    @Autowired
    private KLineTitleService kLineTitleService;
    @Autowired
    private RedisServer redisServer;
    /**
     * K线栏目列表页
     * @return
     */
    @GetMapping("kLinepage")
    public ModelAndView kLineTitlepage(){
        return new ModelAndView("kLineTitle/klinepage");
    }

    /**
     * 查询K线栏目列表
     * @param kLineTitle
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询K线栏目列表",notes = "查询K线栏目列表")
    @PostMapping("klineList")
    public ReturnResult<KLineTitle> klineList(KLineTitle kLineTitle,
                                              @RequestParam("page")Integer pageIndex,
                                              @RequestParam("limit")Integer pageSize){
        PageInfo<KLineTitle> info = kLineTitleService.selectByEntityLists(kLineTitle,pageIndex,pageSize);
        return  new ReturnResult<KLineTitle>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 启用/停用
     * @param kLineTitle
     * @return
     */
    @ApiOperation(value = "K线栏目状态修改",notes = "K线栏目状态修改")
    @PostMapping("enablekline")
    public Object enableKline(KLineTitle kLineTitle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"k线栏目启用/停用");
        if(kLineTitle.getIsEnable()){
            kLineTitle.setIsEnable(false);
        }else {
            kLineTitle.setIsEnable(true);
        }
        kLineTitle.setUpdateTime(new Date());
        KLineTitle old = kLineTitleService.selectByPrimaryKey(kLineTitle.getId());
        kLineTitleService.updateByPrimaryKey(kLineTitle,old);
        return new ReturnResult<KLineTitle>(0,"成功",0,0);
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @GetMapping("eidtkline")
    public Object updateKline(@RequestParam("id")Integer id){
        ModelAndView mv = new ModelAndView("kLineTitle/eidtklinepage");
        KLineTitle kLineTitle = kLineTitleService.selectByEntity(new KLineTitle().setId(id));
        mv.addObject("kLineTitle",kLineTitle);
        return mv;
    }

    /**
     * 编辑方法
     * @param kLineTitle
     * @return
     */
    @ApiOperation(value = "编辑K线栏目",notes = "编辑K线栏目")
    @PostMapping("updatekline")
    public Object updateKlineTitle(KLineTitle kLineTitle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"k线栏目编辑");
        kLineTitle.setUpdateTime(new Date());
        KLineTitle old = kLineTitleService.selectByPrimaryKey(kLineTitle.getId());
        kLineTitleService.updateByPrimaryKey(kLineTitle,old);
        ModelAndView mv = new ModelAndView("kLineTitle/klinepage");
        return mv;
    }

    /**
     * 新增页面
     * @return
     */
    @GetMapping("addkline")
    public Object addkline(){
        ModelAndView mv = new ModelAndView("kLineTitle/klineAdd");
        return mv;
    }

    /**
     * 新增方法
     * @param kLineTitle
     * @return
     */
    @ApiOperation(value = "新增K线栏目",notes = "新增K线栏目")
    @PostMapping("insertkline")
    public Object insertkline(KLineTitle kLineTitle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"k线栏目新增");
        KLineTitle title = kLineTitleService.selectByEntity(kLineTitle);
        if(null != title){
            ModelAndView mv = new ModelAndView("kLineTitle/klinepage");
            return mv;
        }
        kLineTitle.setCreateTime(new Date());
        kLineTitleService.insertSelective(kLineTitle);
        ModelAndView mv = new ModelAndView("kLineTitle/klinepage");
        return mv;
    }
}
